package com.example.yls.orderly_broadcast;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private HighPriorityReceniver highPriorityReceniver;
    private MiddlePriorityReceniver middlePriorityReceniver;
    private LowPriorityReceniver lowPriorityReceniver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1.从高到低创建三个不同等级的广播接收者
        //2.创建完接收者之后，就是对他们进行注册(动态或静态):注册的时候可以为它们指定不同的优先级
        //2-1:动态注册
        highPriorityReceniver=new HighPriorityReceniver();
        IntentFilter highFilter =new IntentFilter();
        highFilter.addAction("ORDERED_BROADCAST");
        highFilter.setPriority(100); //指定优先级 值越大，优先级越高
        registerReceiver(highPriorityReceniver,highFilter);

        middlePriorityReceniver=new MiddlePriorityReceniver();
        IntentFilter middleFilter =new IntentFilter();
        middleFilter.addAction("ORDERED_BROADCAST");
        middleFilter.setPriority(50);
        registerReceiver(middlePriorityReceniver,middleFilter);

        lowPriorityReceniver=new LowPriorityReceniver();
        IntentFilter lowFilter =new IntentFilter();
        lowFilter.addAction("ORDERED_BROADCAST");
        lowFilter.setPriority(0);
        registerReceiver(lowPriorityReceniver,lowFilter);

        findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction("ORDERED_BROADCAST");
                sendOrderedBroadcast(intent,null);//null表示指定的权限
            }
        });
    }
}
